package modeller.databasedesignmodel.servicedemand.transaction;

import modeller.databasedesignmodel.relation.Relation;
import modeller.databasedesignmodel.servicedemand.IServiceDemand;
import modeller.databasedesignmodel.servicedemand.transaction.proceduralstatement.ProceduralStatementServiceDemand;


import java.util.LinkedList;

/**
 * Created by:  Jason Ye
 * Date:        28/05/2012
 * Time:        12:28
 */
public class TransactionServiceDemand implements IServiceDemand {

    LinkedList<IServiceDemand> proceduralStatementServiceDemands;

    public TransactionServiceDemand(LinkedList<IServiceDemand> proceduralStatementServiceDemands) {
        this.proceduralStatementServiceDemands = proceduralStatementServiceDemands;
    }

    public void addProceduralStatementServiceDemand(IServiceDemand demand) {

        if (demand instanceof ProceduralStatementServiceDemand) {
            proceduralStatementServiceDemands.add(demand);
        }

    }

    public void removeProceduralStatementServiceDemand(IServiceDemand demand) {
        proceduralStatementServiceDemands.remove(demand);
    }

    @Override
    public String myToString() {

        StringBuffer buf = new StringBuffer();
        for (IServiceDemand s : proceduralStatementServiceDemands) {
            buf.append(s.myToString());
        }
        return buf.toString();
    }

    @Override
    public double getServiceDemandForRelation(Relation relation) {
        double runningTotal =0;
        for(IServiceDemand ps : proceduralStatementServiceDemands){
            runningTotal+= ps.getServiceDemandForRelation(relation);
        }
        return runningTotal;
    }

    public LinkedList<IServiceDemand> getProceduralStatementServiceDemands() {
        return proceduralStatementServiceDemands;
    }

    public void setProceduralStatementServiceDemands(LinkedList<IServiceDemand> proceduralStatementServiceDemands) {
        this.proceduralStatementServiceDemands = proceduralStatementServiceDemands;
    }
}
